<?php
header('Content-Type:text/html;charset=utf-8');

class Vote extends CI_Controller {
	
	function addvote($wid='')
	{
		if ( $wid == '' )
		{
			$data['cont'] = '-2';	//没有ID
		}
		else
		{
			$t = 5;	//投票间隔时间
			$return = 0;
			$ip = $this->input->ip_address();
			$sql = 'select count(*) as count from vote where wid=' . $wid . ' and ip="'.$ip.'"';
			$voteCount = $this->db->query($sql)->row()->count;
			if( $voteCount > 0 )	//出现过的IP
			{
				$sql = 'select addtime ,cont from vote where  wid=' . $wid . ' and ip="'.$ip.'"';
				$datarow = $this->db->query($sql)->row();
				$addtime = $datarow->addtime;
				$cont = $datarow->cont;
				$len = timeextend(date("Y-m-d H:i:s"),$addtime);
				
				if( $len > $t ){	
					$cont = $cont + 1;
					$data = array(
						'cont' => $cont,
						'addtime' => date("Y-m-d-H-i-s")
					);
					$this->db->where('wid', $wid);
					$this->db->where('ip', $ip);
					$this->db->update('vote', $data); 
					$return = $cont;
					$data['cont'] = $return;
				}
				else	//时间不匹配
				{
					$return = -1;	
					$data['cont'] = $return;
				}
			}
			else	//没有出现过的IP
			{
				$this->db->set("wid",$wid);
				$this->db->set("ip",$ip);
				$this->db->set("cont",1);
				$this->db->set("addtime",date("Y-m-d-H-i-s"));
				$this->db->insert("vote");
				$return = 1;
				$data['cont'] = $return;
			}
			if( $return > -1 )	//操作work表更新
			{
				$sql = 'select sum(cont) as vcont from vote where wid='.$wid;
				$return = $this->db->query($sql)->row()->vcont;
				
				$data = array(
					'cont' => $return
				);
				$this->db->where('id', $wid);
				$this->db->update('work', $data); 
					
			}
		}
		$this->load->view('vote',$data);
	}
}